package huawei.bfs;

import treenode.TreeNode;

import java.util.*;

public class J107 {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> data = new ArrayList<>();
        if (root == null) {
            return data;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int len;
        while ((len = queue.size()) > 0) {
            ArrayList<Integer> low = new ArrayList<>();
            for (int i = 0; i < len; i++) {
                TreeNode tmp = queue.poll();
                low.add(tmp.val);
                if (tmp.left != null) {
                    queue.offer(tmp.left);
                }
                if (tmp.right != null) {
                    queue.offer(tmp.right);
                }
            }
            data.add(0, low);
        }
        return data;
    }
}
